﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using System.Data;
using Yipusoft.Exercitation.DBUtility;
using Yipusoft.SoldierArchives.Model;

public partial class Combat_SkillStat : System.Web.UI.Page
{
    string id = "55F5139F-489A-487D-8D55-FF6E555C7FDA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            InitData();
    }

    private void InitData()
    {
        for (int i = 2010; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        ddlYear.SelectedValue=string.IsNullOrEmpty(Request["Year"])?DateTime.Now.Year.ToString():Request["Year"];
        ddlMonth.SelectedValue = string.IsNullOrEmpty(Request["Year"]) ? DateTime.Now.Month.ToString() : Request["Month"];        

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();
        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));

        if (!string.IsNullOrEmpty(Request["StationID"]))
            ddlStation.SelectedValue = Request["StationID"];

        
        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));

        if (!string.IsNullOrEmpty(Request["RankID"]))
            ddlRank.SelectedValue = Request["RankID"];

        ddlItem.DataSource = ItemSkill.GetList(" ParentID='" + id + "'");
        ddlItem.DataTextField = "ItemSkillName";
        ddlItem.DataValueField = "ID";
        ddlItem.DataBind();
        ddlItem.Items.Insert(0, new ListItem("全部", "0000"));

        string departmentID = string.IsNullOrEmpty(Request["DepartmentID"]) ? Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID : Request["DepartmentID"];
        ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID IN (Select DepartmentID From TaskSquadron Where TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "') Order by DepartmentID");
        ddlDepartment.DataTextField = "ShortName";
        ddlDepartment.DataValueField = "DepartmentID";
        ddlDepartment.DataBind();
        ddlDepartment.SelectedValue = departmentID;
        
        if (!string.IsNullOrEmpty(Request["ItemID"]))
        {
            ddlItem.SelectedValue = Request["ItemID"];
            Bind();
        }
    }
    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + "").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if (!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }

    private void Bind()
    {
        //string departmentID = string.IsNullOrEmpty(Request["DepartmentID"]) ? Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID : Request["DepartmentID"];
        //List<Yipusoft.Account.Model.DepartmentInfo> departmentList = Yipusoft.Account.BLL.Department.GetList(" DepartmentID IN (Select DepartmentID From TaskSquadron Where TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "') Order by DepartmentID");
        DataTable dtb = new DataTable();
        DataColumn clm;
        DataRow drw;
        clm = new DataColumn("SoldierID", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("SoldierName", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("InputScore", typeof(System.Decimal)); //录入率
        dtb.Columns.Add(clm);
        clm = new DataColumn("OrderScore", typeof(System.String)); //录入率
        dtb.Columns.Add(clm);

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
        string where = string.Empty;
        string where2=string.Empty;
        if (!string.IsNullOrEmpty(txtSolderName.Text.Trim()))
            where = " And SoldierName like '%" + txtSolderName.Text + "%'";

        where2=" SoldierID IN( Select SoldierID From FireMis..TaskSoldiers Where DepartmentID='" + ddlDepartment.SelectedValue + "' " + GetRankAndStation() + " And TaskID='" + GetTaskID() + "')" ;

        where2+=where;
        List<Yipusoft.SoldierArchives.Model.SoldierArchiveInfo> soldierList= soldier.GetSoldiersByWhere(where2 ,"SoldierID","Desc",1000,0);        
        foreach (SoldierArchiveInfo info in soldierList)
        {
            drw = dtb.NewRow();
            drw["SoldierID"] = info.SoldierID;
            drw["SoldierName"] = info.SoldierName;
            object[] array = GetScore(info.DepartmentID,info.SoldierID);
            drw["InputScore"] = Math.Round((decimal)array[0], 2);
            drw["OrderScore"] = array[1].ToString();
            dtb.Rows.Add(drw);

        }

        UserGrid.DataSource = dtb.DefaultView;
        UserGrid.DataBind();
        //foreach (Yipusoft.Account.Model.DepartmentInfo dpt in departmentList)
        //{
        //    drw = dtb.NewRow();
        //    drw["DepartmentID"] = dpt.DepartmentID;
        //    drw["DepartmentName"] = dpt.ShortName;
        //    object []array=GetScore(dpt.DepartmentID);
        //    drw["InputScore"] = array[0];
        //    drw["OrderScore"] = array[1].ToString();
        //    dtb.Rows.Add(drw);

        //}
        //UserChart.Series["技能达标率"].Points.DataBindXY(dtb.DefaultView, "SoldierName", dtb.DefaultView, "InputScore");
        //for (int i = 0; i < dtb.Rows.Count; i++)
        //{
        //    UserChart.Series["技能达标率"].Points[i].ToolTip = dtb.Rows[i]["OrderScore"].ToString();
        //}
    }
    public string GetRankID(string soldierID)
    {
        TaskSoldiersInfo soldierInfo = TaskSoldiers.GetTaskSoldiers(soldierID);
        return soldierInfo != null ? soldierInfo.RankID : Guid.Empty.ToString();
    }
    public object[] GetScore(string DepartmentID,string soldierID)
    {
        object[] array = new object[2];
        string itemWhere = string.Empty;
        if (!ddlItem.SelectedValue.Equals("0000"))
            itemWhere = " And SkillID IN (Select SkillID From ItemSkill Where ParentID IN (Select ID From ItemSkill Where ParentID ='" + ddlItem.SelectedValue + "'))";

        string itemWhere2 = string.Empty;
        //if(!string.IsNullOrEmpty(GetRankAndStation()))
        //{
        //    itemWhere2 = " And SoldierID IN (Select SoldierID From TaskSoldiers Where 1=1 " + GetRankAndStation() + "  And DepartmentID='" + DepartmentID + "')";
        //}
        
        //if (!string.IsNullOrEmpty(txtSolderName.Text.Trim()))
        //{
        string whereSoldier = " And SoldierID='" + soldierID + "'";
        //}

        //已录入且已达标的技能
        string sql = "Select Count(*) From TaskScoreView Where IsPass=1 And TaskID='" + GetTaskID() + "' And DepartmentID='" + DepartmentID + "'" + itemWhere + whereSoldier;
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        //应录入的技能数量
        sql = "Select Count(*) From ( Select b.ID,a.DepartmentID,a.SoldierID,a.TaskID,a.RankID,a.StationID From TaskSoldiersView a  join TaskItems  b on a.RankID=b.RankID And a.StationID=b.StationID and a.TaskID=b.TaskID"
            + " And a.TaskID='" + GetTaskID() + "' ) as c  "
            + " join TaskSubject d on c.ID=d.TaskItemID  And  d.TaskID='" + GetTaskID() + "' And DepartmentID= '" + DepartmentID + "'" + itemWhere + whereSoldier;
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        if (count2 > 0)
            array[0]=(count / count2) * 100;
        else
            array[0]= 0;

        array[1] = "合格技能:"+count.ToString()+" 总技能:"+count2.ToString();

        return array;

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        Bind();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text =  (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}
